package com.jhgsys.internal.base.controller;

import com.jhgsys.internal.base.dto.BaseProjectDto;
import com.jhgsys.internal.base.entity.BaseProject;
import com.jhgsys.internal.base.service.IBaseProjectService;
import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.exception.FebsException;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 项目设置 Controller
 *
 * @author Dinghaitao
 * @date 2024-07-29 14:19:15
 */
@Slf4j
@Validated
@Controller
public class BaseProjectController extends BaseController {

    @Autowired
    private IBaseProjectService baseProjectService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "baseProject")
    @RequiresPermissions("baseProject:view")
    public String baseProjectIndex(Model model){
        model.addAttribute("tenantList", getTenantList());
        model.addAttribute("tenantId", getCurrentTenantId());
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        return jumpPage("base/baseProject/baseProjectList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "baseProject/add")
    @RequiresPermissions("baseProject:add")
    public String baseProjectAdd(Model model){
        model.addAttribute("tenantList", getTenantList());
        model.addAttribute("tenantId", getCurrentTenantId());
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        model.addAttribute("baseProject", new BaseProject());
        return jumpPage("base/baseProject/baseProjectEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "baseProject/update/{id}")
    @RequiresPermissions("baseProject:update")
    public String baseProjectUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("baseProject", baseProjectService.findBaseProjectById(id));
        return jumpPage("base/baseProject/baseProjectEdit");
    }

    /**
     * 查询（所有）
     * @author Dinghaitao
     * @date 2024-07-29 14:19:15
     */
    @GetMapping("baseProject")
    @ResponseBody
    @RequiresPermissions("baseProject:view")
    public FebsResponse getAllBaseProjects(BaseProject baseProject) {
        try {
            List<BaseProject> baseProjects = baseProjectService.findBaseProjects(baseProject);
            return success(baseProjects);
        }catch (Exception ex){
            return failure(ex);
        }
    }

    /**
     * 查询（分页）
     * @author Dinghaitao
     * @date 2024-07-29 14:19:15
     */
    @GetMapping("baseProject/list")
    @ResponseBody
    @RequiresPermissions("baseProject:view")
    public FebsResponse baseProjectList(QueryRequest request, BaseProject baseProject) {
        try {
            //没有总公司权限则查询自己公司下的
            if(!hasHeadCompanyRole()){
                baseProject.setTenantId(getCurrentTenantId());
            }
            Map<String, Object> dataTable = getDataTable(this.baseProjectService.findBaseProjects(request, baseProject));
            return success(dataTable);
        }catch (Exception ex){
            return failure(ex);
        }
    }

    /**
     * 新增 BaseProject
     * @author Dinghaitao
     * @date 2024-07-29 14:19:15
     */
    @Log("新增BaseProject")
    @PostMapping("baseProject")
    @ResponseBody
    @RequiresPermissions("baseProject:add")
    public FebsResponse addBaseProject(@RequestBody @Valid BaseProjectDto baseProjectDto) throws FebsException {
        try {
            this.baseProjectService.createBaseProject(baseProjectDto);
            return success();
        } catch (Exception ex) {
            return failure(ex,"新增项目失败！");
        }
    }

    /**
     * 逻辑删除 BaseProject
     * @author Dinghaitao
     * @date 2024-07-29 14:19:15
     */
    @Log("逻辑删除BaseProject")
    @GetMapping("baseProject/delete/{id}")
    @ResponseBody
    @RequiresPermissions("baseProject:delete")
    public FebsResponse deleteBaseProject(@PathVariable("id") String baseProjectIds) throws FebsException {
        try {
            BaseProject baseProject = new BaseProject();
            baseProject.setDeletedBy(getCurrentUser().getUserName());
            baseProject.setDeletedTime(new Date());
            baseProject.setDeletedFlg(FebsConstant.INVALID);
            this.baseProjectService.deleteBaseProject(baseProject,baseProjectIds);
            return success();
        } catch (Exception ex) {
            return failure(ex,"新增项目失败！");
        }
    }

    /**
    * 物理删除 BaseProject
    * @author Dinghaitao
    * @date 2024-07-29 14:19:15
    */
    @Log("物理删除BaseProject")
    @GetMapping("baseProject/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("baseProject:view")
    public FebsResponse physicallyDeleteBaseProject(@PathVariable("id") String baseProjectIds) throws FebsException {
        try {
            this.baseProjectService.physicallyDeleteBaseProject(baseProjectIds);
            return success();
        } catch (Exception ex) {
            return failure(ex,"新增项目失败！");
        }
    }

    /**
     * 修改 BaseProject
     * @author Dinghaitao
     * @date 2024-07-29 14:19:15
     */
    @Log("修改BaseProject")
    @PostMapping("baseProject/update")
    @ResponseBody
    @RequiresPermissions("baseProject:update")
    public FebsResponse updateBaseProject(@RequestBody BaseProjectDto baseProjectDto) throws FebsException {
        try {
            this.baseProjectService.updateBaseProject(baseProjectDto);
            return success();
        } catch (Exception ex) {
            return failure(ex,"更新项目失败！");
        }
    }

    /**
     * 导出 BaseProject的excel
     * @author Dinghaitao
     * @date 2024-07-29 14:19:15
     */
    @PostMapping("baseProject/excel")
    @ResponseBody
    @RequiresPermissions("baseProject:view")
    public void export(QueryRequest queryRequest, BaseProject baseProject, HttpServletResponse response) throws FebsException {
        try {
            List<BaseProjectDto> baseProjects = this.baseProjectService.findBaseProjects(queryRequest, baseProject).getRecords();
            ExcelKit.$Export(BaseProject.class, response).downXlsx(baseProjects, false);
        } catch (Exception ex) {
            failure(ex,"导出项目失败！");
        }
    }
}
